package com.base.system.managedBean;

import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.user.util.Browser;

import com.base.common.config.RepositoryConst;
import com.base.commons.SpringBeanHolder;
import com.base.organization.entity.DepartmentalUnit;
import com.base.organization.service.IDepartmentalUnitDAO;
import com.base.security.SecurityConst;
import com.base.security.SecurityReplace;
import com.base.system.managedBean.baseBean.BaseSecurityDepartmentBean;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_editDepartmentalUnitBean", scope = ManagedBeanScope.REQUEST)
public class EditDepartmentalUnitBean extends BaseSecurityDepartmentBean {
	Log logger = LogFactory.getLog(EditDepartmentalUnitBean.class);
	private IDepartmentalUnitDAO departmentalUnitDAO = SpringBeanHolder
			.getBean("departmentalUnitDAO");
	@Inject
	private FacesContext context;
	@Bind(id = "parentDepartmentalUnitId_name", attribute = "value")
	private String parentDepartmentalUnitId_name;

	protected void initSpecialProperty() {
		Map<String, Object> session = context.getExternalContext()
				.getSessionMap();
		String departmentId = context.getExternalContext()
				.getRequestParameterMap().get("departmentId");
		if (departmentId != null && !"".equals(departmentId)) {
			departmentalUnit = departmentalUnitDAO.getById(Integer
					.parseInt(departmentId));
		}
		if (departmentalUnit == null) {
			departmentalUnit = (DepartmentalUnit) session
					.get(SecurityConst.DEPARTMENTALUNIT_SELECT_ROW_DATA);
		}
		this.getDepartmentProperty();
		parentDepartmentalUnitId_name = departmentalUnit
				.getParentDepartmentalUnitId() == null ? "" : (SecurityReplace
				.replaceDepartmentName(departmentalUnit
						.getParentDepartmentalUnitId().toString()));
	}

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			this.initSpecialProperty();
		}
	}

	@Action(id = "saveDepartmentalUnit")
	public void saveDepartmentalUnit() {
		StringBuffer parentPath = new StringBuffer();
		parentPath = parentPath.append("/").append(
				RepositoryConst.REPOSITORY_DEMANDDOC).append("/");
		departmentalUnit = departmentalUnitDAO.get(Integer
				.parseInt(departmentalUnitId));
		String oldName = departmentalUnit.getName();
		String oldPath = parentPath.toString() + "/" + oldName + "/";
		setSecurityDepartmentProperty();
		departmentalUnitDAO.update(departmentalUnit);
		/*
		 * if (!oldName.equals(departmentalUnit.getName())) { KRepository kr =
		 * SecurityHolder.getRepository(
		 * RepositoryConst.REPOSITORY_RESOUREC_NAME, (HttpSession)
		 * context.getExternalContext() .getSession(false));
		 * kr.editFile(oldPath, departmentalUnit.getName(), parentPath
		 * .toString()); }
		 */
		String script = "closeCenterLayoutTab('"
				+ SecurityConst.DEPARTMENTALUNIT_EDIT_TAB + "')";
		Browser.execClientScript("window.top", script);
	}
}
